package com.yamaikeji.plan.controller;

import com.yamaikeji.common.annotation.Log;
import com.yamaikeji.common.core.controller.BaseController;
import com.yamaikeji.common.core.domain.AjaxResult;
import com.yamaikeji.common.enums.BusinessType;
import com.yamaikeji.plan.aop.CheckLogin;
import com.yamaikeji.system.domain.plan.Memo;
import com.yamaikeji.system.service.plan.IMemoService;
import com.yamaikeji.system.util.IdCreateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 便笺Controller
 * 
 * @author yamaikeji
 * @date 2023-12-10
 */
@RestController
@RequestMapping("/api/plan/memo")
public class MemoController extends BaseController {

    @Autowired
    private IMemoService memoService;

    /**
     * 查询便笺列表
     */
    @CheckLogin
    @PostMapping("/getMemoList")
    public AjaxResult getMemoList(HttpServletRequest req, @RequestBody Memo memo) {
        String userNo = (String) req.getAttribute("userNo");
        memo.setUserNo(userNo);
        startPage();
        List<Memo> list = memoService.selectMemoList(memo);
        return AjaxResult.success(list);
    }

    /**
     * 新增保存便笺
     */
    @CheckLogin
    @Log(title = "便笺", businessType = BusinessType.INSERT)
    @PostMapping("/addMemo")
    public AjaxResult addSave(HttpServletRequest req, @RequestBody Memo memo) {
        memo.setMemoNo(IdCreateUtil.generateNo("M"));
        String userNo = (String) req.getAttribute("userNo");
        memo.setUserNo(userNo);
        memoService.insertMemo(memo);
        return AjaxResult.success(1);
    }

    /**
     * 修改保存便笺
     */
    @CheckLogin
    @Log(title = "便笺", businessType = BusinessType.UPDATE)
    @PostMapping("/editMemo")
    public AjaxResult editSave(@RequestBody Memo memo) {
        memoService.updateMemo(memo);
        return AjaxResult.success(1);
    }

    /**
     * 删除便笺
     */
    @CheckLogin
    @Log(title = "便笺", businessType = BusinessType.DELETE)
    @PostMapping( "/deleteMemo")
    public AjaxResult deleteMemo(@RequestBody Memo memo) {
        memoService.deleteMemoByMemoNo(memo);
        return AjaxResult.success(1);
    }
}
